//
//  SuperMarketViewController.m
//  CommunitySupermarketProiect
//
//  Created by 吕惠卿 on 2017/10/17.
//  Copyright © 2017年 吕惠卿. All rights reserved.
//
#define NAVBAR_COLORCHANGE_POINT (-IMAGE_HEIGHT + NAV_HEIGHT)
#define NAV_HEIGHT 64
#define IMAGE_HEIGHT 200
#define SCROLL_DOWN_LIMIT 70
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define LIMIT_OFFSET_Y -(IMAGE_HEIGHT + SCROLL_DOWN_LIMIT)


#import "SuperMarketViewController.h"
#import "SDCycleScrollView.h"
#import "WRImageHelper.h"
#import "WRNavigationBar.h"
//AFURLRequestSerialization.h
#import <TMT4IOS/AFURLRequestSerialization.h>

//#import "AFURLRequestSerialization.h"
@interface SuperMarketViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout, SDCycleScrollViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong) SDCycleScrollView *advView;
@end
static NSString *homeeightColl = @"homeeightColl";
static NSString *homenineColl = @"homenineColl";
@implementation SuperMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    WhiteColor;
    self.collectionView.contentInset = UIEdgeInsetsMake(IMAGE_HEIGHT-64, 0, 0, 0);
    [self.collectionView addSubview:self.advView];
    [self.view addSubview:self.collectionView];
    [self creatNavigationItem];
}
-(void)creatNavigationItem{
    UIButton *btn = [UIButton new];
    [btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    UIButton *btnright = [UIButton new];
    [btnright setImage:[UIImage imageNamed:@"分类uu"] forState:UIControlStateNormal];
    [btnright addTarget:self action:@selector(btnrightClick) forControlEvents:UIControlEventTouchUpInside];
    [btnright sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnright];
}
//分类
-(void)btnrightClick{
    ClassCommunitySupermarketViewController *classComVC = [[ClassCommunitySupermarketViewController alloc]init];
    [self.navigationController pushViewController:classComVC animated:YES];

}
- (void)btnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -UICollectionViewDelegate
-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) collectionViewLayout:flowLayout];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //背景颜色
        _collectionView.backgroundColor = [UIColor whiteColor];
        //自适应大小
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[HomeEightCollectionViewCell class] forCellWithReuseIdentifier:@"homeeightColl"];
        [_collectionView registerClass:[HomeNineCollectionViewCell class] forCellWithReuseIdentifier:@"homenineColl"];
    }
    return _collectionView;
}
//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HomeEightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeeightColl forIndexPath:indexPath];
        NSString *words = @"——  推荐商品 ——";
        NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]}];
        NSTextAttachment *attatch = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        attatch.bounds = CGRectMake(0, 0, 13, 13);
        attatch.image = [UIImage imageNamed:@"猜你喜欢"];
        NSAttributedString *string8 = [NSAttributedString attributedStringWithAttachment:attatch];
        [strAtt insertAttributedString:string8 atIndex:3];
        cell.label.attributedText = strAtt;
        return cell;
    }
    else if (indexPath.section == 1){
        HomeNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homenineColl forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:@"产品列表图坚果"];
        cell.nameLab.text = @"VANS/烦死男鞋STYLE 36板鞋白蓝白红GD女鞋休闲帆布";
        cell.moneyLab.text = @"￥599";
        cell.numLab.text = @"820人已购买";
        return cell;
    }
    else if (indexPath.section == 2){
        HomeEightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeeightColl forIndexPath:indexPath];
        NSString *words = @"——  全部商品 ——";
        NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]}];
        NSTextAttachment *attatch = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        attatch.bounds = CGRectMake(0, 0, 13, 13);
        attatch.image = [UIImage imageNamed:@"全部商品"];
        NSAttributedString *string8 = [NSAttributedString attributedStringWithAttachment:attatch];
        [strAtt insertAttributedString:string8 atIndex:3];
        cell.label.attributedText = strAtt;
        return cell;
    }
    else{
        HomeNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homenineColl forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:@"产品列表图坚果"];
        cell.nameLab.text = @"VANS/烦死男鞋STYLE 36板鞋白蓝白红GD女鞋休闲帆布";
        cell.moneyLab.text = @"￥599";
        cell.numLab.text = @"820人已购买";
        return cell;
    }
    
}
//定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else if (section == 1){
        return 4;
    }
    else if (section == 2){
        return 1;
    }
    else {
        return 4;
    }
    
}
//机会选中消息
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  YES;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return (CGSize){MCScreenSize.width , 40};
    }
    else if (indexPath.section == 1){
        return (CGSize){MCScreenSize.width/2 , 200};
    }
    else if (indexPath.section == 2){
        return (CGSize){MCScreenSize.width , 40};
    }
    else {
        return (CGSize){MCScreenSize.width/2 , 200};
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  
}



- (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(CGSizeMake(newSize.width*2, newSize.height*2));
    [image drawInRect:CGRectMake (0, 0, newSize.width*2, newSize.height*2)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (SDCycleScrollView *)advView
{
    if (_advView == nil) {
        NSArray *localImages = @[@"社区超市图",@"社区超市图",@"社区超市图",@"社区超市图",@"社区超市图",@"社区超市图",@"社区超市图"];
        _advView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, -IMAGE_HEIGHT, kScreenWidth, IMAGE_HEIGHT) imageNamesGroup:localImages];
        _advView.pageDotColor = [UIColor grayColor];
        _advView.autoScrollTimeInterval = 2;
        _advView.currentPageDotColor = [UIColor whiteColor];
        _advView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    }
    return _advView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
